package cn.com.dbappsecurity.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.com.dbappsecurity.entity.User;
import cn.com.dbappsecurity.frame.annotations.Action;
import cn.com.dbappsecurity.frame.annotations.Auth;
import cn.com.dbappsecurity.frame.annotations.Controller;
import cn.com.dbappsecurity.frame.annotations.Log;
import cn.com.dbappsecurity.frame.annotations.Param;
import cn.com.dbappsecurity.frame.annotations.Validate;
import cn.com.dbappsecurity.frame.datamodal.GridDataModal;
import cn.com.dbappsecurity.frame.datamodal.IDataModal;
import cn.com.dbappsecurity.service.IUser;
import cn.com.dbappsecurity.service.impl.UserImpl;

@Controller("user")
public class UserController extends BaseController{
	
	private IUser userService = getService(UserImpl.class);
	
	/**
	 * 用户登录
	 * @param req
	 * @param name
	 * @param pass
	 * @return
	 * @throws Exception 
	 */
	@Log(value="用户登录",entry={"name=用户名","password=用户密码"})
	@Action
	public boolean login(HttpServletRequest req,
			@Validate(regex="[\\w]{3,20}")String name,
			@Validate(regex="[\\w]{6,16}")@Param("password")String password) throws Exception{
		boolean success = userService.login(req.getSession(),name,password);
		return success;
	}
	
	/**
	 * 用户登出
	 * @param req
	 * @param name
	 * @param pass
	 * @return
	 * @throws Exception 
	 */
	@Log(value="用户登出")
	@Action
	public boolean logout(HttpServletRequest req) throws Exception{
		req.getSession().invalidate();
		return true;
	}
	
	/**
	 * 添加用户
	 * @param name
	 * @param pass
	 * @return
	 * @throws Exception
	 */
	@Action
	@Log(value="添加用户",entry={"name=用户名","password=用户密码"})
	@Auth("ADD_USER")
	public boolean insert(@Validate(regex="[\\w]{3,20}")String name,
			@Validate(regex="[\\w]{6,16}")String password)throws Exception{
		return userService.insert(name,password);
	}
	
	@Action
	@Log(value="删除用户")
	@Auth("DELETE_USER")
	public boolean delete(@Validate(minLen=1)Integer[] ids)throws Exception{
		return userService.delete(ids);
	}
	
	@Action
	@Log(value="修改用户",entry={"name=用户名","password=用户密码"})
	@Auth("UPDATE_USER")
	public boolean update(@Validate(minLen=1)int id,String name,String password)throws Exception{
		return userService.update(id, name, password, null);
	}
	
	@Action
	@Log(value="查询用户",entry={"name=用户名","createTimeFrom=创建时间从","createTimeTo=创建时间到",
			"loginTimeFrom=最后登录时间从","loginTimeTo=最后登录到"})
	@Auth("SEARCH_USER")
	public IDataModal search(String name,String createTimeFrom,String createTimeTo,
			String loginTimeFrom,String loginTimeTo,int page,int rows,String sort,String order)throws Exception{
		int total = userService.searchCount(name, createTimeFrom, createTimeTo, loginTimeFrom, loginTimeTo);
		List<User> data = null;
		if(total > 0){
			data = userService.search(name, createTimeFrom, createTimeTo, loginTimeFrom, loginTimeTo,page,rows,sort,order);
		}
		IDataModal modal = new GridDataModal(total, data);
		return modal;
	}
	
}
